package com.example.sls.service.impl.admin.defaultProSet;

import com.example.sls.mapper.executeSQL.ExecuteSQLMapper_admin;
import com.example.sls.mapper.admin.defaultProSet.DefaultProSetMapper;
import com.example.sls.pojo.admin.defaultProSet.DefaultProSet;
import com.example.sls.service.admin.defaultProSet.DefaultProSetService;
import com.example.sls.utils.ConstUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DefaultProSetServiceImpl implements DefaultProSetService {

    @Autowired
    private DefaultProSetMapper defaultProSetMapper;  //用于对习题集进行操作
    @Autowired
    private ExecuteSQLMapper_admin executeSQLMapper;  //用于获取答案

    @Override
    //params 包含 : proType,content,correctSQL(用户从前端输入的)
    public Boolean addPro(Integer proType, String content, String correctSQL) {

        boolean flag = false;

        Integer id = defaultProSetMapper.getMaxId() + 1;
        System.out.println("========");
        System.out.println("impl-id: " + id);
        System.out.println("impl-content: " + content);
        System.out.println( "impl-correctSQL: " + correctSQL);

        String result = executeSQL(correctSQL);
        System.out.println("impl-result: " + result);
        System.out.println("========");
        if(result == null){
            return flag;
        }
        DefaultProSet set = new DefaultProSet(id, proType, content, correctSQL, result);
        if(defaultProSetMapper.addPro(set) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean deleteProById(Integer id) {
        boolean flag = false;
        if(defaultProSetMapper.deleteProById(id) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean updatePro(Integer id, String content, String correctSQL) {
        boolean flag = false;
        Map<String,Object>pro = new HashMap<>();
        String result = executeSQL(correctSQL);
        pro.put("id", id);
        pro.put("content", content);
        pro.put("correctSQL", correctSQL);
        pro.put("result", result);

        if(defaultProSetMapper.updatePro(pro) > 0){
            flag = true;
        }

        return flag;
    }

    @Override
    public DefaultProSet getProInfoById(Integer id) {
        return defaultProSetMapper.getProInfoById(id);
    }

    @Override
    public DefaultProSet getProById(Integer id) {
        return defaultProSetMapper.getProById(id);
    }

    @Override
    public Integer getCount() {
        return defaultProSetMapper.getCount();
    }

    @Override
    public Integer getMaxId() {
        return defaultProSetMapper.getMaxId();
    }

    @Override
    public PageInfo<DefaultProSet> getAllProByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DefaultProSet> sets = defaultProSetMapper.getAllPro();
        return  new PageInfo<DefaultProSet>(sets);
    }

    @Override
    public PageInfo<DefaultProSet> getAllPros4StuByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DefaultProSet> sets = defaultProSetMapper.getAllPros4Stu();
        return new PageInfo<>(sets);
    }

    @Override
    public PageInfo<DefaultProSet> getPageInfoByProType(Integer proType, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DefaultProSet> pros = defaultProSetMapper.getAllProsByProType(proType);
        return new PageInfo<>(pros);
    }

    @Override
    public PageInfo<DefaultProSet> getPageInfo4StuByProType(Integer proType, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DefaultProSet> pros = defaultProSetMapper.getAllPros4StuByProType(proType);
        return new PageInfo<>(pros);
    }



    public String executeSQL(String correctSQL){

        String result = null ;
        System.out.println("========");
        System.out.println("correctSQL.startsWith: " +(correctSQL.startsWith("select") || correctSQL.startsWith("SELECT")) );

        if(correctSQL.startsWith("select") || correctSQL.startsWith("SELECT")){
            String condition = correctSQL.split("\\s+")[1]; //剔除用户输入的多个空格
            System.out.println("condition: " + condition);
            System.out.println("========");
            if(condition.equals("*")){  //查询所有
                String temp = String.valueOf(executeSQLMapper.queryExecuteAll(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("sum") || condition.startsWith("SUM")){  //求和
                String temp = String.valueOf(executeSQLMapper.queryExecuteSum(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("count") || condition.startsWith("COUNT")){  //求数量
                String temp = String.valueOf(executeSQLMapper.queryExecuteCount(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("name") || condition.startsWith("NAME")
                    || condition.startsWith("idcard") || condition.startsWith("IDCARD")
                    || condition.startsWith("address") || condition.startsWith("ADDRESS")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteStr(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("age") || condition.startsWith("AGE")
                    || condition.startsWith("gender")||condition.startsWith("GENDER")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteInt(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("entrydate") || condition.startsWith("ENTRYDATE")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteDate(correctSQL));
                result = executeJudge(temp);
            }
        }
        else{
            result = null;
        }
        return result;
    }

    public String executeJudge(String temp){
        String result;
        if(temp == null){
            result = ConstUtil.MISS;
        }
        else{
            result = temp;
        }
        return result;
    }

}
